﻿using Infrastructures;
using IRepository;
using Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConcreteService
{
    public class UtilService : CommonService
    {
        #region School

        public School CreateSchool(School obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateSchool :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateSchool(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
            return obj;
        }
        public School UpdateSchool(School obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateSchool :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateSchool(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Update, false);
            return obj;
        }
        public School GetSchool(int schoolno)
        {
            if (schoolno == 0)
                throw new Exception("UtilService.UpdateSchool :: parameter is null");
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            School school = repo.GetSchool(schoolno);

            school.Province = AppConfig.GetAreaName(school.ProvinceNO);
            school.City = AppConfig.GetAreaName(school.CityNO);
            school.County = AppConfig.GetAreaName(school.CountyNO);

            return school;
        }
        public IList<School> ListSchool()
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            IList<School> lst = repo.ListSchool();
            foreach (School school in lst)
            {
                school.Province = AppConfig.GetAreaName(school.ProvinceNO);
                school.City = AppConfig.GetAreaName(school.CityNO);
                school.County = AppConfig.GetAreaName(school.CountyNO);
            }
            return lst;
        }

        #endregion

        #region EventLog

        public EventLog CreateEventLog(EventLog obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateEventLog :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateEventLog(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
            return obj;
        }
        public EventLog UpdateEventLog(EventLog obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateEventLog :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateEventLog(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Update, false);
            return obj;
        }
        public EventLog GetEventLog(int id)
        {
            if (id == 0)
                throw new Exception("UtilService.UpdateEventLog :: parameter is null");
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.GetEventLog(id);
        }
        public IList<EventLog> ListEventLog()
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListEventLog();
        }

        #endregion

        #region BaseData

        public BaseData CreateBaseData(BaseData obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateBaseData :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateBaseData(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
            return obj;
        }
        public BaseData UpdateBaseData(BaseData obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateBaseData :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateBaseData(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Update, false);
            return obj;
        }
        public BaseData GetBaseData(int ruleno)
        {
            if (ruleno == 0)
                throw new Exception("UtilService.UpdateBaseData :: parameter is null");
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.GetBaseData(ruleno);
        }
        public IList<BaseData> ListBaseData()
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListBaseData();
        }

        #endregion

        #region Relationship

        public Relationship CreateRelationship(Relationship obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateRelationship :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateRelationship(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
            return obj;
        }
        public Relationship UpdateRelationship(Relationship obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateRelationship :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateRelationship(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Update, false);
            return obj;
        }
        public Relationship GetRelationship(int id)
        {
            if (id == 0)
                throw new Exception("UtilService.UpdateRelationship :: parameter is null");
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.GetRelationship(id);
        }
        public IList<Relationship> ListRelationship()
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListRelationship();
        }
        public IList<Relationship> ListRelationship(RelationshipFun type, int toNO)
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListRelationship(type, toNO);
        }

        #endregion

        #region Lifecycle

        public LifecycleEvent CreateLifecycleEvent(LifecycleEvent obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateLifecycleEvent :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateLifecycleEvent(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
            return obj;
        }
        public LifecycleEvent UpdateLifecycleEvent(LifecycleEvent obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateLifecycleEvent :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateLifecycleEvent(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Update, false);
            return obj;
        }
        public LifecycleEvent GetLifecycleEvent(int id)
        {
            if (id == 0)
                throw new Exception("UtilService.UpdateLifecycleEvent :: parameter is null");
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.GetLifecycleEvent(id);
        }
        public IList<LifecycleEvent> ListLifecycleEvent()
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListLifecycleEvent();
        }

        #endregion

        #region FileSystem

        public FileSystem CreateFileSystem(FileSystem obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateFileSystem :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateFileSystem(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
            return obj;
        }
        public FileSystem UpdateFileSystem(FileSystem obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateFileSystem :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateFileSystem(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Update, false);
            return obj;
        }
        public FileSystem GetFileSystem(int fileno)
        {
            if (fileno == 0)
                throw new Exception("UtilService.UpdateFileSystem :: parameter is null");
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.GetFileSystem(fileno);
        }
        public IList<FileSystem> ListFileSystem()
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListFileSystem();
        }

        public IList<FileSystem> ListFileSystem(FileType type, int belongno)
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListFileSystem(type, belongno);
        }


        #endregion

        #region Money

        public Money CreateMoney(Money obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateMoney :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateMoney(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
            return obj;
        }
        public Money UpdateMoney(Money obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateMoney :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateMoney(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Update, false);
            return obj;
        }
        public IList<Money> ListMoney()
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListMoney();
        }
        public MoneyLog CreateMoneyLog(MoneyLog obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateMoneyLog :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateMoneyLog(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
            return obj;
        }
        public MoneyLog UpdateMoneyLog(MoneyLog obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateMoneyLog :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateMoneyLog(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
            return obj;
        }
        public IList<MoneyLog> ListMoneyLog()
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListMoneyLog();
        }

        #endregion

        #region Splite

        public SpliteWord CreateSpliteWord(SpliteWord obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateSpliteWord :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateSpliteWord(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
            return obj;
        }
        public SpliteWord UpdateSpliteWord(SpliteWord obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateSpliteWord :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateSpliteWord(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Update, false);
            return obj;
        }
        public SpliteWord GetSpliteWord(int id)
        {
            if (id == 0)
                throw new Exception("UtilService.UpdateSpliteWord :: parameter is null");
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.GetSpliteWord(id);
        }
        public IList<SpliteWord> ListSpliteWord()
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListSpliteWord();
        }
        public void SpliteWord(SpliteWordFun fun)
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            var dic = repo.ListWrodToSpliteByType(fun);
            foreach (var item in dic)
            {
                ISegmentation seg = ServiceFactory.CreateInstance<ISegmentation>();
                SpliteWord word = new SpliteWord();
                word.Fun = fun;
                word.ID = item.Key.ToInt();
                word.SpliteValue = seg.Segment(item.Value);
                word.Value = item.Value;

                CreateSpliteWord(word, SystemConfig.Get("SysEventID"));
            }
        }

        public void SpliteWord(SpliteWordFun fun, int id, string words)
        {
            ISegmentation seg = ServiceFactory.CreateInstance<ISegmentation>();
            SpliteWord word = new SpliteWord();
            word.Fun = fun;
            word.ID = id;
            word.SpliteValue = seg.Segment(words);
            word.Value = words;

            CreateSpliteWord(word, SystemConfig.Get("SysEventID"));
        }

        #endregion

        #region SeldomUsed

        public SeldomUsed CreateSeldomUsed(SeldomUsed obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateSeldomUsed :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateSeldomUsed(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
            return obj;
        }
        public IList<SeldomUsed> ListSeldomUsed(int belongID, SeldomType type, APPRole role)
        {
            if (belongID == 0)
                throw new Exception("UtilService.UpdateSeldomUsed :: parameter is null");
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListSeldomUsed(belongID, type, role);
        }
        public SeldomUsed UpdateSeldomUsed(SeldomUsed obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateSeldomUsed :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateSeldomUsed(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Update, false);
            return obj;
        }
        public SeldomUsed GetSeldomUsed(int id)
        {
            if (id == 0)
                throw new Exception("UtilService.UpdateSeldomUsed :: parameter is null");
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.GetSeldomUsed(id);
        }
        public IList<SeldomUsed> ListSeldomUsed()
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListSeldomUsed();
        }

        #endregion

        #region Validation

        public Validation CreateValidation(Validation obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateValidation :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateValidation(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
            return obj;
        }
        public Validation UpdateValidation(Validation obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateValidation :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateValidation(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Update, false);
            return obj;
        }
        public Validation GetValidation(int id)
        {
            if (id == 0)
                throw new Exception("UtilService.UpdateValidation :: parameter is null");
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.GetValidation(id);
        }
        public IList<Validation> ListValidation()
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListValidation();
        }

        #endregion

        #region Area

        public Area CreateArea(Area obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateArea :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateArea(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
            return obj;
        }
        public Area UpdateArea(Area obj, string oper)
        {
            if (obj == null || oper == null)
                throw new Exception("UtilService.UpdateArea :: parameter is null");

            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            obj = repo.CreateArea(obj);
            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Update, false);
            return obj;
        }
        public Area GetArea(int code)
        {
            if (code == 0)
                throw new Exception("UtilService.UpdateArea :: parameter is null");
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.GetArea(code);
        }
        public IList<Area> ListArea()
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListArea();
        }

        #endregion

        #region UserMessage

        public void CreateUserMessage(UserMessage obj, string oper)
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            repo.CreateUserMessage(obj);

            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Create, false);
        }

        public void UpdateUserMessage(UserMessage obj, string oper)
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            repo.UpdateUserMessage(obj);

            ProcessHelper.ProcessAfterDone(obj, oper.ToString(), EventType.Update, false);
        }

        public JObject ListUserMessage(string userNo, int start, int limit)
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListUserMessage(userNo, start, limit);
        }

        public UserMessage GetUserMessage(int id)
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.GetUserMessage(id);
        }

        #endregion

        #region Eventlog

        public JObject ListEventLog(IDictionary<SearchKey, string> dicCondition, int start, int limit)
        {
            IUtilRepository repo = ServiceFactory.CreateInstance<IUtilRepository>();
            return repo.ListEventLog(dicCondition, start, limit);
        }

        #endregion
    }
}
